Base R Object: ts

Regularly spaced time series data can be represented as objects of class ts. R modeling functions that work with ts objects include arima, forecast, strucchange, etc.

Creating a ts object:

y <- ts(data = 1:12, frequency = 12, start=c(1990,1), end=c(1990,12))

ts objects have their own plot method (plot.ts).

# sunspots is a `ts` object
plot(sunspots, main = "Monthly Sunspot Numbers, 1749-1983") 

To subset a ts object and preserve the date information, use the window() function.

window(sunspots, start = c(1950,1), end = c(1950,12))
##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
## 1950 101.6  94.8 109.7 113.4 106.2  83.6  91.0  85.2  51.3  61.4  54.8  54.1

Creating lagged data:

stats::lag(x)  # lag the data by 1 period
stats::lag(x, k = 12)  # lag the data by 12 periods

Creating differences:

base::diff(x)  # first difference
base::diff(x, lag=12) # annual difference

To combine multiple ts objects into a single object, use the cbind() function. The combined object is assigned the additional class mts (multiple time series).

z <- cbind(x, y)

Plot of mts objects puts each series in a separate panel.

plot(cbind(sunspots, base::diff((sunspots))), main = "Sunspots")

Ordered Observations: zoo

The ts class is quite limited. Irregular spacing data cannot be represented using the ts object.

The zoo (abbr. Zeileis’ Ordered Observations) class was designed to handle time series data with an arbitrary ordered time index. A zoo object essentially attaches date information stored in a vector with data information stored in a matrix.

library(zoo)

To create a zoo object one needs an ordered time index and data. Typically, the time index is an object of class Date, POSIXct, yearmon, yearqtr, or timeDate.

t <- seq(as.Date("1993/3/1"),  as.Date("2003/3/1"), "months")
x <- cumsum(rnorm(length(t)))
z <- zoo(x = x, order.by = t)

Creating zoo object from ts:

sunspots_zoo <- as.zoo(sunspots)
class(sunspots_zoo)
## [1] "zooreg" "zoo"

The plot() function can be used to plot zoo objects.

plot(sunspots_zoo, main="Sunspots, 1749-1983")

The time index and data can be extracted using the index() and coredata() functions.

index(x)
coredata(x)

To combine multiple zoo objects, use either cbind() or merge(). Use cbind() when combining zoo objects with the same time index. Use merge() when the objects have different time indexes.

merge(x, y) # default outer join
merge(x, y, join="inner")
merge(x, y, join="right", fill=na.locf) # last observation carry forward

Dealing with missing values:

na.locf(xts_object, na.rm = TRUE, fromLast = FALSE, maxgap = Inf) 
na.fill(object, fill, ...)  # replacing NAs
na.trim(object, ...)  # trimming NAs
na.omit(object, ...)  # removing NAs
na.approx(object, ...)  # linear interpolation

Reading and writing zoo series:

read.zoo("foo.csv", FUN = as.yearmon, index.column = 1)
read.zoo("foo.tab", format = "%m/%d/%Y")

tseries is a time series analysis and computational finance package that works with zoo objects.

library(tseries)

Download historical stock quote from Yahoo Finance:

sbux_c <- get.hist.quote(instrument = "MSFT", quote = "Close", retclass = "zoo")
## time series ends   2021-03-19
plot(sbux_c)

Extensible Time Series: xts

xts (eXtensible Time Series) is a powerful package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo.

library(xts)

Creating xts object:

y <- xts(x = 1:100, order.by = Sys.Date() + 1:100)

Converting to xts:

sunspots_xts <- as.xts(sunspots)
plot(sunspots_xts)

xts object also works with ggplot2.

library(ggplot2)
autoplot(sunspots_xts)

Subsetting:

sunspots_xts["1900"]
sunspots_xts["1900/1901-07"]
sunspots_xts["1900-03/1901-07"]

Periodicity:

periodicity(sunspots_xts)
to.yearly(sunspots_xts)  # convert to yearly OHLC
to.period(sunspots_xts, period = "years") 

Aggregating:

sunspots_xts_yr <- apply.yearly(sunspots_xts, FUN = mean)
plot(sunspots_xts_yr)

Continuous rolling windows:

sunspots_xts_ma <- rollapply(sunspots_xts, width=120, FUN=mean)
plot(merge(sunspots_xts, sunspots_xts_ma))

Tidy Time Series: tsibble

tsibble is a variation of tidy data object tibble that deals with temporal data.

library(fpp3)  # a short cut to load all tidy-related packages
## ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
## ✓ tibble      3.0.4       ✓ tsibble     0.9.3  
## ✓ dplyr       1.0.2       ✓ tsibbledata 0.2.0  
## ✓ tidyr       1.1.2       ✓ feasts      0.1.7  
## ✓ lubridate   1.7.9.2     ✓ fable       0.3.0
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## x lubridate::date()   masks base::date()
## x dplyr::filter()     masks stats::filter()
## x dplyr::first()      masks xts::first()
## x tsibble::index()    masks zoo::index()
## x tsibble::interval() masks lubridate::interval()
## x dplyr::lag()        masks stats::lag()
## x dplyr::last()       masks xts::last()

Creating tsibble object:

y <- tsibble(
  Year = 2015:2019,
  Observation = c(123, 39, 78, 52, 110),
  index = Year
)

Converting to tsibble object:

z %>%
  mutate(Month = yearmonth(Month)) %>%
  as_tsibble(index = Month)

Generating white-noise:

tsibble(period = 1:100, wn = rnorm(100), index = period) %>% autoplot(wn)

Filtering on index:

aus_production %>%
  filter_index("1970 Q1" ~ "2004 Q4")

Aggregating over calendar periods:

vic_elec %>%
  index_by(date = ~ as_date(.)) %>% 
  summarise(
    ttl_demand = sum(Demand, na.rm = T),
    avg_temp = mean(Temperature, na.rm = T)
    )

Tidy time series plot:

ansett %>% 
  filter(Airports == "MEL-SYD") %>%
  autoplot(Passengers)

Seasonal plots:

PBS %>%
  filter(ATC2 == "A10") %>%
  select(Month, Concession, Type, Cost) %>%
  summarise(TotalC = sum(Cost)) %>%
  mutate(Cost = TotalC / 1e6) -> a10

a10 %>% gg_season(Cost) 

Seasonal subseries plots:

a10 %>% gg_subseries(Cost)

Correlation matrix:

library(GGally)
tourism %>%
  group_by(State) %>%
  summarise(Trips = sum(Trips)) %>%
  pivot_wider(values_from=Trips, names_from=State) %>%
  GGally::ggpairs(columns = 2:9)

Lag plots:

aus_production %>%
  filter(year(Quarter) >= 2000) %>% 
  gg_lag(Beer, geom = "point")

Autocorrelation and partial-autocorrelation:

aus_production %>% feasts::ACF(Beer) %>% autoplot()

aus_production %>% feasts::PACF(Beer) %>% autoplot()

Reference

Hyndman, R.J., & Athanasopoulos, G. (2021) Forecasting: principles and practice, 3rd edition, OTexts: Melbourne, Australia. OTexts.com/fpp3.

Eric Zivot (2014) Working with Financial Time Series Data in R.